(function() {
	var $ = require(L('$')), _ = require(L('_'));

	var win = $.createWindow(L('win_forgot_password'));
	var ui = $.layout('forgot_password');
	win.add([$.ui('navbar')(true, false), ui.view]);
	var txtFirstName = $.ui('text_field').create(L('hint_first_name'), Ti.UI.TEXT_AUTOCAPITALIZATION_WORDS);
	var txtEmail = $.ui('text_field').create(L('hint_email'));
	ui.container.add([txtFirstName, txtEmail]);
	ui.view.add($.icon_info());
	$.tap(ui.btn_submit, function() {
		var error = null, first_name = txtFirstName.val(), email = txtEmail.val();
		if ($.isNull(first_name))
			error = L('err_first_name_required');
		else if ($.isNull(email) || !$.isValidEmail(email))
			error = L('err_login_email_invalid');
		if (!$.isNull(error))
			$.a(error, L('message_error_title'));
		else {
			$.mask();
			$.webservice().emailNewPassword(first_name, email, function(e) {
				$.mask(false);
				var result = JSON.parse(e);
				if (result.Success === false) {
					$.a(result.Error, L('message_error_title'));
				} else {
					$.soft_indicator().show(result.MessageSuccess);
					_.delay(function() {
						$.soft_indicator().hide();
						$.win('home').nav.pop();
					}, 1500);
				}
			});
		}
	});
	win.reset = function(){
		$.emptyTextFields([txtFirstName, txtEmail]);
	};
	$.on('focus', win, function() {
		$.ui('text_field').init([txtFirstName, txtEmail], ui.view, $.mainContentHeight(true));
	});

	module.exports = win;
})();
